import xml.sax
import argparse

import codecs


class GiftHandler(xml.sax.ContentHandler):
    def __init__(self):
        self.Gift = ""
        self.Guard = ""
        self.dict = {"gift": 0, "guard": 0, "sc": 0}
        self.SC = set()
        self.GiftNum = set()
        self.GuardNum = set()

    def startDocument(self):
        pass

    # 元素开始事件处理
    def startElement(self, tag, attributes):
        # self.Gift = tag
        if tag == "gift":
            user = attributes["user"]
            giftname = attributes["giftname"]
            self.dict["gift"] += 1
            if giftname != "辣条":
                print("*****礼物*****", file=f)
                self.GiftNum.add(user)
                print("user:", user, end=" ", file=f)
                print("giftname:", giftname, file=f)
        elif tag == "guard":
            print("*****舰长*****", file=f)
            self.dict["guard"] += 1
            user = attributes["user"]
            level = attributes["level"]
            self.GuardNum.add(user)
            print("user:", user, end=" ", file=f)
            print("level:", level, file=f)
        elif tag == "sc":
            print("*****SC*****", file=f)
            self.dict["sc"] += 1
            user = attributes["user"]
            price = attributes["price"]
            self.SC.add(user)
            print("user:", user, end=" ", file=f)
            print("price:", price, file=f)

    def endDocument(self):
        print("*****礼物统计*****", file=f)
        print("赠送礼物数（不统计重复人员）:", len(self.GiftNum), file=f)
        print("*****赠送礼物人员名单:", file=f)
        print('/**/'.join(list(self.GiftNum)), file=f)
        print("*****SC统计*****", file=f)
        print("本次SC数（不统计重复人员）:", len(self.SC), file=f)
        print("*****SC人员名单:", file=f)
        print('\n'.join(list(self.SC)), file=f)
        print("*****舰长统计*****", file=f)
        print("本次上舰数（不统计重复人员）:", len(self.GuardNum), file=f)
        print("*****本次舰长名单:", file=f)
        print('\n'.join(list(self.GuardNum)), file=f)


if __name__ == "__main__":
    parser = xml.sax.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces, 0)
    Handler = GiftHandler()
    parser.setContentHandler(Handler)
    # filename = input()
    parser1 = argparse.ArgumentParser(description='Search some files')
    parser1.add_argument('-i', '-o', dest='outfile', action='append', help='选择输入输出文件名')
    args = parser1.parse_args()
    f = codecs.open(args.outfile[1], "w", "utf-8")
    parser.parse(args.outfile[0])
    f.close()
    # parser.parse("7.10.xml")
